package com.zuo.demo01.controller.admin;

import com.alibaba.fastjson.JSON;
import com.zuo.demo01.pojo.Blog;
import com.zuo.demo01.pojo.Catagory;
import com.zuo.demo01.service.BlogService;
import com.zuo.demo01.service.CatagoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private CatagoryService catagoryService;


    @RequestMapping("/table/list/blog")
    @ResponseBody
    public Map<String,Object> blogs_list(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit){

        Map<String,Object> map1 = new HashMap<>();
        map1.put("startIndex",(page-1)*limit);
        map1.put("pageSize",limit);

        List<Blog> list = blogService.findAll(map1);

        Map<String,Object> map2=new HashMap<>();
        map2.put("code",0);
        map2.put("msg","");
        map2.put("count",blogService.count());
        map2.put("data",list);
        return map2;
    }

    @RequestMapping("/table/list/blog/{key}")
    @ResponseBody
    public Map<String,Object> blogs_list(@PathVariable("key") String key,@RequestParam("page") Integer page,@RequestParam("limit") Integer limit){

        Map<String,Object> map1 = new HashMap<>();
        map1.put("startIndex",(page-1)*limit);
        map1.put("pageSize",limit);
        map1.put("key",key);
        //System.out.println("doge"+key+"doge");

        List<Blog> list = blogService.findByKey(map1);

        Map<String,Object> map2=new HashMap<>();
        map2.put("code",0);
        map2.put("msg","");
        map2.put("count",blogService.countByKey(key));
        map2.put("data",list);
        return map2;
    }


    @RequestMapping("/blog/insert")
    @ResponseBody
    public String save(Blog blog,@RequestParam("categoryId") Integer categoryId,@RequestParam("tags") String tags
                       ,@RequestParam("status") Integer status,@RequestParam("encomment") Integer encomment,@RequestParam("token") Integer token){
        Map<String,Object> map = new HashMap<>();


        map.put("title",blog.getTitle());
        map.put("url",blog.getUrl());
        map.put("image",blog.getImage());
        map.put("content",blog.getContent());
        map.put("catagoryId",categoryId);
        map.put("tags",tags);
        map.put("status",status);
        map.put("views",0);
        map.put("encomment",encomment);

        if(token != null && token==1){
            map.put("id",blog.getId());
            return blogService.update(map);
        }else{
            return blogService.insert(map);
        }
    }






    @RequestMapping("/blog/delete")
    @ResponseBody
    public String blog_delete(@RequestBody List<Blog> blogs){
        Map<String,String> map = new HashMap<>();

        int result = 1;
        for (Blog blog:
             blogs) {
            result = blogService.delete(blog.getId());
            if(result==0){
                map.put("status","error");
                break;
            }
        }
        if(result==1){
            map.put("status","success");
        }
        String remap = JSON.toJSONString(map);
        return remap;
    }


    @RequestMapping("/blog/update/{id}")
    public String go_blog_update(@PathVariable("id") Integer id, Model model){
        List<Catagory> categories = catagoryService.findAll();
        model.addAttribute("categories",categories);

        Blog blog = blogService.getById(id);

        model.addAttribute("blog",blog);
        model.addAttribute("token",1);

        return "admin/edit";
    }




}
